
window.addEvent("domready", function()
{
  var Sol = new Sector("Sol", [], [], [], [], [], "Sol", new Pricelist([], []));
  Sol.tiles = Global.CachedSectors["Sol"];
  Global.SectorList[Global.CurrentSector] = Sol;
  $("MapArea").empty();
  $("MapArea").adopt(Sol.toDOM(Panels.Main.TileAddition));
});

Panels.Main.DisplaySaved = function(result)
{
  var maps = result["Sectors"];
  var select = $("SavedMaps");
  maps.sort();

  for (var i = 0; i < maps.length; i++)
  {
    var map = maps[i];
    
    new Element("option", { value: map, html: map })
        .addEvent("click", function() { $("MapName").set("value", this.value); })
        .inject(select, "bottom");
  }
}

EventSink.addEvent(MsgLogin, Panels.Main.DisplaySaved);

Panels.Main.New = function()
{
  SelectSectorWindow.Show(function(sector)
  {
    var options = $("SavedMaps").getElements("option");

    for (var i = 0; i < options.length; i++)
    {
      if (options[i].get("value") == sector)
      {
        alert("Map name must be unique!");
        return;
      }
    }
    Global.SectorList[0] = sector;
    $("MapArea").empty();
    $("MapArea").adopt(sector.toDOM(Panels.Main.TileAddition));

    Utility.Server.AddSector(sector.name, Utility.Sector.Export(), (function(result, error)
    {
      if (error)
        return;

      new Element("option", { value: this.sector.name, html: this.sector.name })
      .addEvent("click", function() { $("MapName").set("value", this.value); })
      .inject($("SavedMaps"), "bottom");

      Global.SectorList[0] = this.sector;
      $("MapArea").empty();
      $("MapArea").adopt(sector.toDOM(Panels.Main.TileAddition));

    }).bind({ sector: sector }));
  });
}

Panels.Main.Save = function()
{
  var name = $("MapName").get("value");
  var code = Utility.Sector.Export();
  var maps = $("SavedMaps").getElements("option");
  var element = $("SavedMaps").getSelected()[0];

  if (!$chk(element))
    return;

  if (name == element.get("value")) // Same name
  {
    Utility.Server.UpdateSector(name, code, $empty); // We don't need to do anything here
  }
  else
  {
    if (sectors.get("value").contains("name"))
    {
      alert("Sector name must be unique!");
      return;
    }

    Utility.Server.RenameSector(element.get("value"), name, (function(result, error)
    {
      if (error)
        return;

      Global.SectorList[Global.CurrentSector].name = this.name;
      this.element.set("value", this.name).set("html", this.name);

      Utility.Server.UpdateSector(this.name, this.code, $empty); // Nothing to be done here

    }).bind({ name: name, element: element, code: code }));
  }
}

Panels.Main.Load = function()
{
  var selected = $("SavedMaps").getSelected()[0];

  if (!$chk(selected))
    return;

  var name = selected.get("value");

  Utility.Server.GetSector(name, (function(result)
  {
    sector = Utility.Sector.Import(result["Code"]);
    Global.SectorList[Global.CurrentSector] = sector;
    $("MapArea").empty();
    $("MapArea").adopt(sector.toDOM());
    Utility.Sector.Draw();
    
  }).bind({ name: name }));
}

Panels.Main.Delete = function()
{
  var selected = $("SavedMaps").getSelected()[0];

  if (!$chk(selected))
    return;

  if (!confirm("Are you sure you want to delete this sector?"))
    return;

  var name = selected.get("value");

  Utility.Server.DeleteSector(name, (function(result, error)
  {
    if (error)
      return;

    this.selected.destroy();
  }).bind({ selected: selected }));
}

Panels.Main.Import = function()
{
  var code = prompt("Paste the sector code here:");
  if (!($chk(code) && $chk(code.trim())))
    return;

  var map = JSON.decode(code).map;
  
  Utility.Server.GetSectorCode(map, (function(result, error)
  {
    try
    {
      var sector = Utility.Sector.Import(this.code);
      $("MapArea").empty();
      $("MapArea").adopt(sector.toDOM(Panels.Main.TileAddition));
      Global.SectorList[Global.CurrentSector] = sector;
      Utility.Sector.Draw();
    }
    catch(error)
    {
      alert("Error Loading Sector");
    }
  }).bind({code: code}));
}

Panels.Main.Export = function()
{
  prompt("Sector Code:", Utility.Sector.Export());
}